#include "pubbase.h"

#define MAX_BUFFER 40960

int ReadCfg(char *epacFileName, char *epacSection, char *epacKey, char *opacValue, int eiFlag)
{ 
    char buf[MAX_BUFFER];
    char sec_txt[MAX_BUFFER];
    char *ptr;
    int find=0, mval;
    FILE *fp = NULL;
    char aczFileName[256];

    assert(epacFileName != NULL && epacKey != NULL);
    snprintf(sec_txt, sizeof(sec_txt), "[%s]", epacSection);

    //if( epacFileName[0] == '/' )
    sprintf( aczFileName, "%s", epacFileName );

    fp = fopen( aczFileName, "r" );
    if( fp == NULL )
    {
        tool_log( "[%s][%d]: open file [%s] err!\n",
            __FILE__, __LINE__, aczFileName );
        return -1;
    }

    if ( 1 == eiFlag )
    {
        rewind(fp);
        while ( fgets(buf, sizeof(buf), fp) )
        {
            /* filter the blank before */
            for (ptr=buf; *ptr==' '; ptr++);

            /* skip the comment and empty line */
            if (*ptr == 10   ||
                *ptr == 13   ||
                *ptr == '#' ||
                *ptr == '\\' ||
                strstr(ptr, sec_txt) == NULL)
            {
                continue;
            }

            /* epacSection found */
            find = 1;
            break;
        } /* end of while (fgets(buf, sizeof(buf), fp)) */
    } /* end of if ( 1 == eiFlag ) */

    if ( 1 == eiFlag && !find) 
    {
        fclose(fp);
        return -1;
    }

    mval = 0;
    while ( fgets(buf, sizeof(buf), fp) )
    {
        /* filter the blank before */
        for (ptr=buf; *ptr==' '; ptr++);

        /* find the epacKey until reach the next epacSection if not a multi-opacValue */
        if (*ptr == '[')
        {
            /* point to the end of the file for do not read anymore */
            fseek(fp, 0, SEEK_END);
            fclose(fp);
            return mval == 0 ? -2 : 0;
        } /* end of if (*ptr == '[') */

        /* skip empty line and comment and not include the epacKey line */
        if (*ptr == 10   ||
            *ptr == 13   ||
            *ptr == '#' ||
            *ptr == '\\' ||
            (mval == 0 && (ptr = strstr(ptr,epacKey)) == NULL)) 
            continue;

        /* if multi-opacValue will do not need local the value */
        if ( 0 == mval )
        {
            /* local the symbol '=' */
            for (ptr+=strlen(epacKey); *ptr == ' '; ptr++);

            if (*ptr != '=')  
                continue;
    
            /* local the opacValue start */
            for (; *(++ ptr) == ' '; );

            if (*ptr == 13 || *ptr == 10 || *ptr == 0) 
            {
                fclose(fp);
                return -3;
            }
        } /* end of if ( 0 == mval ) */

        /* mark as opacValue end when meet a comment or end of line */
        for (find=0; *(ptr+find) && *(ptr+find) != '#' ; find++);

        /* skip the blank or enter(Win:1310/Unix:10) after opacValue */
        for (; *(ptr+find-1) == ' ' ||
             *(ptr+find-1) == 10 ||
             *(ptr+find-1) == 13;
             find --);

        /* whether multi-opacValue */
        if (*(ptr+find-1) == '\\')
        {
            mval ++;
            if (*(ptr+find-2) == ' ')
                *(ptr+find-1) = 0x0;
            else
            {
                *(ptr+find-1) = 0x20;
                *(ptr+find) = 0x0;
            }
        } 
        else 
            *(ptr+find) = 0x0;
    
        if (mval != 0)
        {
            strcat(opacValue, ptr);
            continue;
        } 

        *(ptr+find) = 0x0;
        strcpy(opacValue, ptr);
        fclose(fp);
        return 0;
    } /* end of while ( fgets(buf, sizeof(buf), fp) ) */

    fclose(fp);
    return mval == 0 ? -2 : 0;
}
